var Q = require('q'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection'),
	stationWorkController = require('../../../Initialize/server/controllers/StationWork.server.controller');

exports.findByTerminalID = function(terminalID, completeFun){
	var entity = entityCollection.getEntity("StationTerminal");
	entity.findOne({Terminal: terminalID}, [{path: 'Terminal'}, {path: 'WorkLocation'}], function(err, record){
		if(err){
			var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工位终端交叉表错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
		}
		else{
			completeFun(null, record);
		}
	});
};

// 根据工位和终端查找业务状态
exports.findBusinessStatus = function (workLocationId, terminalId) {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("StationTerminal");
    entity.findOne({WorkLocation: workLocationId, Terminal: terminalId}, null, function (err, record) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工位终端交叉表错误';
            newErr.message = '请检查查询参数！';
            deferred.reject(newErr);
            return;
        }
        if (record === null) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工位终端交叉表错误';
            newErr.message = '没有找到符合条件的记录';
            deferred.reject(newErr);
            return;
        }
        deferred.resolve(record.Status);
    });

    return deferred.promise;
};

// 根据终端ID查询一条记录
exports.findOne = function (terminalId) {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("StationTerminal");
    entity.Entity
    .findOne({Terminal: terminalId}, { WorkLocation: 1 })
    .exec(function (err, record) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工位终端错误';
            newErr.message = '查询工位终端错误，请检查查询参数！';
            deferred.reject(newErr);
            return;
        }
        deferred.resolve(record);
    });

    return deferred.promise;
};

// 新增一条工位终端记录
exports.insert = function (record) {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("StationTerminal");
    entity.insert(record, function (err, newRecord) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '新增工位终端错误';
            newErr.message = '新增工位终端失败';
            deferred.reject(newErr);
            return;
        }
        deferred.resolve(newRecord);
    });

    return deferred.promise;
};

// 更新用户上岗记录
exports.update = function (record) {
    var deferred = Q.defer();

    var entity = entityCollection.getEntity("StationTerminal");
    entity.update(record, function(err, result) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '更新工位终端错误';
            newErr.message = '更新工位终端失败';
            deferred.reject(newErr);
            return;
        }
        deferred.resolve(result);
    });

    return deferred.promise;
};